package com.hyb.shiro;

import com.hyb.Util.Md5Util;
import com.hyb.entity.Staff;
import com.hyb.service.StaffService;
import com.hyb.token.JwtToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * <p>
 *      自定义密码校验器
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Component
public class MyCredentialsMatcher extends SimpleCredentialsMatcher {
    @Autowired
    private StaffService staffService;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        JwtToken jwtToken = (JwtToken) token;
        if (jwtToken.getPassword()==null){
            return true;
        }
        String inPassword = new String(jwtToken.getPassword());

        String staffEmail = String.valueOf(info.getPrincipals());
        String dbPassword = (String) info.getCredentials();

        Staff staff = staffService.getStaffBuyStaffEmail(staffEmail);
        String staffSalt = staff.getStaffSalt();

//        System.out.println(this.equals(Md5Util.md5(inPassword + staffSalt) , dbPassword));

        return this.equals(Md5Util.md5(inPassword + staffSalt) , dbPassword);
    }
}
